package com.example.testproject.call_back

import android.graphics.Canvas
import android.util.Log
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.testproject.adapter.CardStackAdapter
import com.example.testproject.ext.toDip
import kotlin.math.sqrt

/**
 *
 * @ClassName: SlideCardStackCallBack
 * @Author: android 超级兵
 * @CreateDate: 10/21/21$ 3:31 PM$
 * TODO
 */
class SlideCardStackCallBack(val adapter: CardStackAdapter<Int>) :
/*
 * 作者:android 超级兵
 * 创建时间: 10/21/21 3:41 PM
     public static final int UP = 1;  // 1

    public static final int DOWN = 1 << 1; // 2

    public static final int LEFT = 1 << 2; //4

    public static final int RIGHT = 1 << 3; // 8
 */
// 1 上滑动
// 3 上下滑动
// 7 上下左滑动
// 15   上下左右滑动
// dragDirs 拖拽
// swipeDirs 滑动
    ItemTouchHelper.SimpleCallback(0, 15) {


    /*
     * 作者:android 超级兵
     * 创建时间: 11/6/21 3:11 PM
     * TODO  拖拽使用,不管
     */
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder,
    ): Boolean = let {
        false
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 10/21/21 3:45 PM
     * TODO 动画结束后的处理
     */
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direct: Int) {
        Log.i("szjDirect", "direct:$direct")

        // 当前滑动的View
        val layoutPosition = viewHolder.layoutPosition
        Log.i("szjLayoutPosition", "layoutPosition:$layoutPosition")
        // 删除当前滑动的元素
        val removeAt = adapter.data.removeAt(layoutPosition)

        // 添加到集合第0个位置 造成循环滑动的效果
        adapter.data.add(0, removeAt)
        adapter.notifyDataSetChanged()

    }


    /*
     * 作者:android 超级兵
     * 创建时间: 11/2/21 5:10 PM
     * TODO 早onDraw时候调用
     */
    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean,
    ) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)

        val maxDistance = recyclerView.width / 2

        //sqrt 开根号
        val sqrt = sqrt((dX * dX + dY * dY).toDouble())

        Log.i("szjDxDy", "Dx${dX}\tDy:${dY}")

        // 放大系数
        var d = sqrt / maxDistance

        if (d > 1) {
            d = 1.0
        }

        val childCount = recyclerView.childCount
        // 循环所有数据
        repeat(childCount) {
            val view = recyclerView.getChildAt(it)

            val valueDip = view.context.toDip(20f)

            val level = childCount - 1 - it
            if (level > 0) {
                if (level < 3) {
                    view.translationY =
                        (valueDip * level - d * valueDip).toFloat()
                    view.scaleX = (1 - 0.05 * level + d * 0.05).toFloat()
                    view.scaleY = (1 - 0.05 * level + d * 0.05).toFloat()
                }
            }
        }

    }

    /*
     * 作者:android 超级兵
     * 创建时间: 10/21/21 4:08 PM
     * TODO  设置回弹距离
     */
//    override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
//        return 0.6f
//    }

    /*
     * 作者:android 超级兵
     * 创建时间: 10/21/21 4:08 PM
     * TODO 设置回弹时间
     */
//    override fun getAnimationDuration(
//        recyclerView: RecyclerView,
//        animationType: Int,
//        animateDx: Float,
//        animateDy: Float
//    ): Long {
//        return 1000
//    }
}